const express = require('express');
const app = express();
const port = 3003;

app.use(express.json());

// 添加日志中间件
app.use((req, res, next) => {
  console.log(`[Server3:${port}] ${new Date().toISOString()} - ${req.method} ${req.url}`);
  next();
});

// 健康检查端点
app.get('/health', (req, res) => {
  res.json({ status: 'healthy', server: 'server3', port, timestamp: new Date().toISOString() });
});

// 主页
app.get('/', (req, res) => {
  res.json({ 
    message: '欢迎访问后端服务器3', 
    server: 'server3',
    port,
    timestamp: new Date().toISOString(),
    headers: req.headers
  });
});

// API端点
app.get('/api/users', (req, res) => {
  res.json([
    { id: 5, name: '孙七', server: 'server3' },
    { id: 6, name: '周八', server: 'server3' }
  ]);
});

app.get('/api/status', (req, res) => {
  res.json({
    server: 'server3',
    port,
    uptime: process.uptime(),
    memory: process.memoryUsage(),
    timestamp: new Date().toISOString()
  });
});

// 模拟延迟端点
app.get('/api/slow', (req, res) => {
  const delay = Math.random() * 1500 + 300; // 300-1800ms随机延迟
  setTimeout(() => {
    res.json({ 
      message: '这是服务器3的慢接口',
      server: 'server3',
      delay: Math.round(delay),
      timestamp: new Date().toISOString()
    });
  }, delay);
});

app.listen(port, '0.0.0.0', () => {
  console.log(`测试服务器3运行在 http://0.0.0.0:${port}`);
});